package com.godofball.medtrack.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.godofball.medtrack.mapper.DrugMapper;
import com.godofball.medtrack.pojo.Drug;
import com.godofball.medtrack.service.DrugService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.File;

@Service
public class DrugServiceImpl extends ServiceImpl<DrugMapper, Drug> implements DrugService {

    @Value("${MedStack.image.drug_path}")
    private String imagePath;

    @Autowired
    DrugMapper drugMapper;

    @Override
    public void updateWithDeleteImage(Drug drug) {
        Long drugId = drug.getId();
        Drug oldDrug = this.getById(drugId);
        String oldImage = oldDrug.getImage();

        this.updateById(drug);

        if(!oldImage.equals(drug.getImage())){
            File oldImagePath = new File(imagePath + oldImage);
            if(oldImagePath.exists())oldImagePath.delete();
        }

    }



}
